﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class GoodsClassAuthority
    {
        private readonly Chain.IDAL.GoodsClassAuthority dal = new Chain.IDAL.GoodsClassAuthority();

        public int Add(Chain.Model.GoodsClassAuthority model)
        {
            return this.dal.Add(model);
        }

        public List<Chain.Model.GoodsClassAuthority> DataTableToList(DataTable dt)
        {
            List<Chain.Model.GoodsClassAuthority> list = new List<Chain.Model.GoodsClassAuthority>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.GoodsClassAuthority item = new Chain.Model.GoodsClassAuthority();
                    if ((dt.Rows[i]["ClassAuthorityID"] != null) && (dt.Rows[i]["ClassAuthorityID"].ToString() != ""))
                    {
                        item.ClassAuthorityID = int.Parse(dt.Rows[i]["ClassAuthorityID"].ToString());
                    }
                    if ((dt.Rows[i]["ClassID"] != null) && (dt.Rows[i]["ClassID"].ToString() != ""))
                    {
                        item.ClassID = int.Parse(dt.Rows[i]["ClassID"].ToString());
                    }
                    if ((dt.Rows[i]["ShopID"] != null) && (dt.Rows[i]["ShopID"].ToString() != ""))
                    {
                        item.ShopID = int.Parse(dt.Rows[i]["ShopID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int ClassAuthorityID)
        {
            return this.dal.Delete(ClassAuthorityID);
        }

        public int DeleteAuthority(int ClassID, int ShopID)
        {
            return this.dal.DeleteAuthority(ClassID, ShopID);
        }

        public bool DeleteList(string ClassAuthorityIDlist)
        {
            return this.dal.DeleteList(ClassAuthorityIDlist);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public int GetClassCountByParentID(int ParentID)
        {
            return this.dal.GetClassCountByParentID(ParentID);
        }

        public int GetClassCountByParentID(int ParentID, int ShopID)
        {
            return this.dal.GetClassCountByParentID(ParentID, ShopID);
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public Chain.Model.GoodsClassAuthority GetModel(int ClassAuthorityID)
        {
            return this.dal.GetModel(ClassAuthorityID);
        }

        public List<Chain.Model.GoodsClassAuthority> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public DataSet GetShopIDListByClass(int ClassID)
        {
            return this.dal.GetShopIDListByClass(ClassID);
        }

        public int SyncGoodsClass(int ClassID)
        {
            Chain.Model.GoodsClass model = new Chain.BLL.GoodsClass().GetModel(ClassID);
            if (model == null)
            {
                return 0;
            }
            if (model.ParentID != 0)
            {
                this.dal.SyncGoodsClass(model.ParentID);
            }
            return this.dal.SyncGoodsClass(ClassID);
        }

        public int SyncGoodsClass(int ClassID, List<int> ShopList)
        {
            Chain.Model.GoodsClass model = new Chain.BLL.GoodsClass().GetModel(ClassID);
            if ((model == null) || (ShopList.Count <= 0))
            {
                return -1;
            }
            if (model.ParentID != 0)
            {
                this.dal.SyncGoodsClass(model.ParentID, ShopList);
            }
            return this.dal.SyncGoodsClass(ClassID, ShopList);
        }

        public int SyncGoodsClass(int ClassID, int ShopID)
        {
            Chain.Model.GoodsClass model = new Chain.BLL.GoodsClass().GetModel(ClassID);
            if (model == null)
            {
                return 0;
            }
            if (model.ParentID != 0)
            {
                this.dal.SyncGoodsClass(model.ParentID, ShopID);
            }
            return this.dal.SyncGoodsClass(ClassID, ShopID);
        }

        public bool Update(Chain.Model.GoodsClassAuthority model)
        {
            return this.dal.Update(model);
        }
    }
}

